<template>
  <view>
     <view  v-for="(item,i) in goodslist" :key="i" @click="gotoDetail(item)">
           <my-goods :goodinfo="item"></my-goods>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
          // 三个入口跳转到商品列表
           // 1.首页楼层
           // 2.搜索历史
           // 3.分类
          queryObj :{
             query:'',
             pagenum:1, // 页码
             pagesize:10,
             cid:'', // 分类id
          },
          goodslist:[], // 商品列表
          total:0, // 总数量
          isLoading:false
         
      };
    },
    onLoad (options) {
       const { query, cid } = options;
       this.queryObj.query = query;
       this.queryObj.cid = cid;
       
       if (!this.queryObj.query) {
          delete this.queryObj.query
       }
       
       if (!this.queryObj.cid) {
          delete this.queryObj.cid;
       }
       this.getGoodsList();
    },
    onReachBottom () {
       if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.showMsg('数据加载完毕');
       if (this.isLoading) return; 
       this.queryObj.pagenum +=1;
       this.getGoodsList()
    },
    onPullDownRefresh () {
        this.queryObj.pagenum = 1;
        this.queryObj.goodslist = [];
        this.queryObj.isLoading = false;
        this.queryObj.total =0;
        this.getGoodsList(function () {
            uni.stopPullDownRefresh()
        })
    },
    methods: {
      gotoDetail (item) {
         uni.navigateTo({
            url:'/subpkg/good_detail/good_detail?goods_id='+ item.goods_id
         })
      },
       async getGoodsList (callback) {
          this.isLoading = true;
          const {data: res} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj);
          callback && callback();
          this.isLoading = false;
          if (res.meta.status !== 200)  return uni.$showMsg();
          this.goodslist = [...this.goodslist, ...res.message.goods];
          this.total = res.message.total;
       }
    }
  }
</script>

<style lang="scss">

</style>
